class Request {
    /* api 请求封装 */
    api(method, url, data, header = {}, isToken = false) {
        // console.log(import.meta.env)
        // console.log(uni.getStorageSync('token'))
        let result = null
        // 如果超出100ms 出现加载loading
        setTimeout(() => {
            if (!result) {
                uni.showLoading({ title: '加载中...' })
            }
        }, 100)
        var headerParams = {
            'content-type': 'application/json;charset=utf-8',
        }
        headerParams = Object.assign(headerParams ? headerParams : {}, header)
        return new Promise((resolve, reject) => {
            uni.request({
                url: import.meta.env.VITE_BASE_URL + url,
                timeout: 60000,
                header: {
                    headerParams,
                    Authorization:
                        isToken == false
                            ? 'Bearer ' + uni.getStorageSync('token')
                            : '',
                },
                data: { ...data },
                method: method,
                success: (res) => {
                    result = { ...res.data }
                    if (result.code != 200) {
                        if (result.code == 401) {
                            uni.removeStorageSync('token')
                            uni.reLaunch({ url: '/pages/login/login' })
                            uni.showToast({
                                title:
                                    result.msg ||
                                    '无效的会话，或者会话已过期，请重新登录。',
                                icon: 'none',
                            })
                            return reject(new Error(result.msg))
                        }
                        uni.showToast({
                            title: result.msg,
                            icon: 'none',
                            duration: 2000,
                        })
                        return reject(new Error(result.msg))
                    }
                    resolve(result)
                },
                fail: (err) => {
                    uni.showToast({
                        title: '请检查网络',
                        icon: 'none',
                    })
                    return reject(new Error(err.errMsg))
                },
                complete: () => {
                    uni.hideLoading()
                },
            })
        })
    }
    /* post请求 */
    post(obj) {
        return this.api('POST', obj.url, obj.data, obj.header, obj.isToken)
    }
    /* get请求 */
    get(obj) {
        return this.api('GET', obj.url, obj.data, obj.header, obj.isToken)
    }
    /* put请求 */
    put(obj) {
        return this.api('PUT', obj.url, obj.data, obj.header, obj.isToken)
    }
    /* del请求 */
    del(obj) {
        return this.api('DELETE', obj.url, obj.data, obj.header, obj.isToken)
    }
    upload(files, data) {
        uni.showLoading({
            title: '上传中...',
        })
        return new Promise((resolve, reject) => {
            uni.uploadFile({
                url: import.meta.env.VITE_BASE_URL + 'common/common-upload',
                // url: import.meta.env.VITE_BASE_URL + '/common/common-uploadAsyn',
                files,
                timeout: 60000 * 3,
                formData: { ...data, noZip: 1, type: `wmsj/${data.type}` },
                success: (res) => {
                    if (res.statusCode != 200) {
                        uni.showToast({
                            title: '系统错误，请联系管理员',
                            icon: 'none',
                        })
                        return reject(new Error('系统错误，请联系管理员'))
                    }
                    let result = JSON.parse(res.data)
                    if (result.code != 0) {
                        uni.showToast({
                            title: result.msg,
                            icon: 'none',
                        })
                        return reject(new Error(result.msg))
                    }
                    resolve(JSON.parse(res.data))
                },
                fail: (error) => {
                    uni.showToast({
                        title: '请检查网络',
                        icon: 'none',
                    })
                    reject(new Error(error))
                },
                complete: () => {
                    uni.hideLoading()
                },
            })
        })
    }
}

export const http = new Request()
